package com.lcpress.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lcpress.entity.User;
import com.lcpress.service.impl.Admin_UserOpServiceImpl;
import com.lcpress.tools.ServletUtil;
import com.lcpress.tools.ValidatorUtil;

@Controller
@RequestMapping(value = "/adminuserop")
public class Admin_UserOpController {

	@Autowired
	Admin_UserOpServiceImpl auserop;

	@RequestMapping(value = "/login")
	public String login() {
		return "admin/admin_login";
	}

	// 用户登录
	@RequestMapping(value = "/login_action", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> login_action(@RequestParam(value = "username", required = true) String username,
			@RequestParam(value = "password", required = true) String password, HttpServletRequest request) {

		User user;
		// 验证是否是手机号
		if (ValidatorUtil.isMobile(username)) {
			user = auserop.getOne(Wrappers.<User>lambdaQuery().eq(User::getPhonenum, username));
		} else {
			user = auserop.getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, username));
		}

		if (user != null) {
			if (password.equals(user.getPassword())) {

				if (user.getUsertype() == 1) {
					return ServletUtil.responseData(0, "您还不是创作者！");
				}

				HttpSession session = request.getSession();
				session.setAttribute("loginuser", user);

				return ServletUtil.responseData(1, "登录成功！");
			}
			return ServletUtil.responseData(0, "密码验证失败！");
		}

		return ServletUtil.responseData(0, "找不到当前用户！");
	}

	// 获取登录用户信息
	@RequestMapping(value = "/getuserinfo_action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> getuserinfo_action(HttpServletRequest request) {

		Map<String, Object> result = new HashMap<>();

		HttpSession session = request.getSession();
		User user = (User) request.getSession().getAttribute("loginuser");

		result.put("username", user.getUsername());
		result.put("phone", user.getPhonenum());
		result.put("issuper", user.getIssuper());

		return result;
	}

	// 登出
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String logout_action(HttpServletRequest request) {

		HttpSession session = request.getSession();
		session.invalidate();

		return "redirect:login";
	}

	/**
	 * 以下为超级管理员功能部分
	 */

	// 超级用户-用户管理
	@RequestMapping("/super_user_list")
	public String super_user_list(HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");

		return "admin/super_user_list";
	}

}
